package com.snow.useviewdemo.util;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.snow.common.tool.utils.BitMapUtils;
import com.snow.common.tool.utils.ToastUtils;
import com.snow.useviewdemo.R;


/**
 * Created by zyt on 2018/8/9.
 * TextView工具类
 */

public class TextViewUtils {
    private static TextViewUtils textViewUtils;

    public static TextViewUtils init() {
        if (textViewUtils == null) {
            synchronized (TextViewUtils.class) {
                if (textViewUtils == null) {
                    textViewUtils = new TextViewUtils();
                }
            }
        }
        return textViewUtils;
    }

    /**
     * 动态设置字体大小
     *
     * @param context
     * @param textView
     * @param size     R.dimen.xxxx
     */
    public void setTextViewTextSize(Context context, TextView textView, int size) {
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getApplicationContext().getResources().getDimension(size));
    }


    /**
     * 获取dp的值===动态设置宽高
     *
     * @param dpValue R.dimen.xxxx
     * @return
     */
    public int getDpValue(Context context, int dpValue) {
        return context.getApplicationContext().getResources().getDimensionPixelOffset(dpValue);
    }

    /**
     * 获取Typeface===动态设置字体
     *
     * @param name dfgb_y7.ttf
     * @return
     */
    public Typeface getTypeface(Context context, String name) {
        return Typeface.createFromAsset(context.getAssets(), "fonts/" + name);
    }

    /**
     * dp 2 px
     *
     * @param dpVal 直接传数字
     */
    public int dp2px(Context ctx, int dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, ctx.getResources().getDisplayMetrics());
    }

    public int sp2px(int sp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
                Resources.getSystem().getDisplayMetrics());
    }

    /**
     * 数字转化为dp
     *
     * @param px
     * @return
     */
    public int num2Dip(Context mContext, int px) {
        return mContext.getResources().getDimensionPixelSize(R.dimen.margin_01) * px;
    }

    /**
     *  * 控件背景色选择器
     *  *
     *  * @param context   当前上下文
     *  * @param idNormal  默认图片
     *  * @param idPressed 按压时图片
     *  * @return
     *  
     */
    public StateListDrawable setSelectorBackgroud(Context context, int idNormal, int idPressed) {
        StateListDrawable bg = new StateListDrawable();
        Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal);
        Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed);
        bg.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);
        bg.addState(new int[]{android.R.attr.state_enabled}, normal);
        bg.addState(new int[]{}, normal);
        return bg;
    }


    /**
     *  * 控件字体颜色选择器
     *  *
     *  * @param mContext 当前上下文
     *  * @param normal   默认显示颜色
     *  * @param pressed  按压后显示的颜色
     *  * @return
     *  
     */
    public ColorStateList createColorStateList(Context mContext, int normal, int pressed) {
        int pressColor = mContext.getResources().getColor(pressed);
        int normalColor = mContext.getResources().getColor(normal);
        int[] colors = new int[]{pressColor, pressColor, normalColor, pressColor, pressColor, normalColor};
        int[][] states = new int[6][];
        states[0] = new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled};
        states[1] = new int[]{android.R.attr.state_enabled, android.R.attr.state_focused};
        states[2] = new int[]{android.R.attr.state_enabled};
        states[3] = new int[]{android.R.attr.state_focused};
        states[4] = new int[]{android.R.attr.state_window_focused};
        states[5] = new int[]{};
        ColorStateList colorList = new ColorStateList(states, colors);
        return colorList;
    }

    public int dpToPx3(int value) {
        return value * 3;
    }
}
